<?php

# Blog db interface. See blog.php.

# Singleton pattern over PDO.
class Db {
	# If using SQLite, enter the full path to your database file.  The folder 
	# that houses the database file must be writeable.  Create the db dir and 
	# chmod to give write permission to the web server user.  For security 
	# reasons, you should make sure that the database file is not stored in a 
	# location accessible from the web.
	# Arch
	#const FILE = '/srv/users-http/ant/db/myblog.sq3';
	# Mac
	const FILE = '/Users/ant/db/myblog.sq3';

	# SECURITY note: please change these.
	const USER = 'admin';
	const PASS = 'mypass';

	# Hold an instance of the class
	private static $instance;

	# A private constructor; prevents direct creation of object.
	private function __construct() {}

	# The singleton method
	public static function singleton() {
		if (! isset(self::$instance)) {
			# Catch exceptions implicity.
			Error::set_exception_handler();

			# Connecting to database.
			# USER and PASS are not used for sqlite driver
			# but are here if driver is changed
			self::$instance = new PDO('sqlite:' . self::FILE, self::USER,
				self::PASS, array(PDO::ATTR_PERSISTENT => TRUE));
		}
	
		return self::$instance;
	}

	# Prevent users to clone the instance.
	public function __clone() {
		Error::trigger('Clone is not allowed.', E_USER_ERROR);
	}
}

# End of file
# vim: set ts=2 sw=2 noet:
?>
